// -------------------------------------------------
// BlinKit - BkBase Library
// -------------------------------------------------
//   File Name: request_impl_apple.cpp
// Description: RequestImpl Class
//      Author: Ziming Li
//     Created: 2022-09-23
// -------------------------------------------------
// Copyright (C) 2022 MingYang Software Technology.
// -------------------------------------------------

#include "./request_impl.h"

#include <CFNetwork/CFNetwork.h>
#include "third_party/zed/include/zed/apple/cf_wrappers.hpp"

std::optional<CURLProxy> RequestImpl::GetSystemProxy(void) const
{
    zed::cf_dictionary_ref settings(CFNetworkCopySystemProxySettings());

    int enable;
    if (!settings.get_value(kCFNetworkProxiesHTTPEnable, enable) || 0 == enable)
        return std::nullopt;

    std::string proxy;
    if (!settings.get_value(kCFNetworkProxiesHTTPProxy, proxy) || proxy.empty())
        return std::nullopt;

    int port;
    if (!settings.get_value(kCFNetworkProxiesHTTPSPort, port))
        return std::nullopt;

    proxy.push_back(':');
    proxy.append(std::to_string(port));
    return std::make_pair(CURLPROXY_HTTP, proxy);
}
